package com.example.demo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.demo.config.ZuWangWebSocket;
import com.example.demo.mapper.DataMonitorMapper;
import com.example.demo.mapper.DeviceMapper;
import com.example.demo.mapper.ExcelMapper;
import com.example.demo.pojo.RestResponse;
import com.example.demo.pojo.excel.Excel;
import com.example.demo.util.CheckDeviceState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/excel")
@CrossOrigin
public class ExcelController {

    @Autowired
    private ZuWangWebSocket webSocket;

    @Autowired
    private DeviceMapper deviceMapper;


    @PostMapping("/uploadExcel")
    public RestResponse uploadExcel(@RequestParam("file") MultipartFile file) {

        if (file.isEmpty()) {
            return RestResponse.validfail("文件为空");
        }

        try {
            // 获取文件名
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> maps = reader.readAll();
            System.out.println(maps);
            List<Excel> errorList = new ArrayList<>();
            for (Map<String, Object> map : maps) {

                Excel excel = new Excel();
                excel.setId(String.valueOf(UUID.randomUUID()));
                excel.setDevice((String) map.get("设备编号"));
                excel.setSn((String) map.get("SN"));
                excel.setIp((String) map.get("IP地址"));
                DateTime dateTime = (DateTime) map.get("更新时间");
                excel.setDatetime(dateTime.toString());

                //如果原来有这个设备了
                if (excelMapper.selectExcelByUniIp(excel) == null) {
                    errorList.add(excel);
                    continue;

                }
                CheckDeviceState.setExcelName(excel.getDevice(),excel);
                excelMapper.insertExcel(excel);

            }
            if (!errorList.isEmpty()){
                String collect = errorList.stream().map(p -> p.getDevice() + " " + p.getSn()).collect(Collectors.joining());
                return RestResponse.validfail(collect +":设备重复");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return RestResponse.success();
    }

    @PostMapping("/deletExcel")
    public RestResponse deletDevice(@RequestBody Excel excel) throws InterruptedException {

        excelMapper.deleteExcel(excel);
        return RestResponse.success();
    }

    @PostMapping("/loadExcelData")
    public RestResponse loadExcelData() {

        List<Excel> excels = excelMapper.readExcel();

        System.out.println("list = " + excels);
        return RestResponse.success(excels);

    }



    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DataMonitorMapper dataMonitorMapper;

    @Autowired
    private ExcelMapper excelMapper;

    @Autowired
    ConfigFileUtil configFileUtil;


}
